package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.http.model.MineMessageModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.MineMessageViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;

import butterknife.BindView;
import butterknife.OnClick;

public class MineMessageActivity extends BaseActivity implements OnItemClickListener {




    @BindView(R.id.gift_listview)
    RecyclerView recyclerView;
    @BindView(R.id.activity_title)
    TextView titleView;

    private BaseRecyclerViewAdapter<MineMessageViewHolder, MineMessageModel> giftAdapter;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initAdapter();
        titleView.setText("我的消息");
//        setContentView(R.layout.activity_account_login);
        EasyHttp.post(this).api(new CommonApi("api/msg")).request(new HttpCallbackProxy<HttpListData<MineMessageModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<MineMessageModel> result, boolean cache) {

                super.onHttpSuccess(result, cache);
                giftAdapter.resetAdapterData(result.getData().getList());

            }
        });
    }

    @Override
    protected void reInitDate() {
        EasyHttp.post(this).api(new CommonApi("api/msg")).request(new HttpCallbackProxy<HttpListData<MineMessageModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<MineMessageModel> result, boolean cache) {

                super.onHttpSuccess(result, cache);
                giftAdapter.resetAdapterData(result.getData().getList());

            }
        });
    }

    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_mine_gift;
    }

    private void initAdapter(){
        giftAdapter = new BaseRecyclerViewAdapter<MineMessageViewHolder, MineMessageModel>() {
            @NonNull
            @Override
            public MineMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_mine_message, null, false);
                MineMessageViewHolder holder=new MineMessageViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull MineMessageViewHolder holder, int position) {
                holder.setDate(getItem(position));

            }
        };
        giftAdapter.setOnItemClickListener(this);
        giftAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        layoutManager.setOrientation(RecyclerView.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(giftAdapter);

    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
        Intent intent= new Intent(this,DocumentActivity.class);
        intent.putExtra("message_id",giftAdapter.getItem(position).getId());
        startActivity(intent);
    }
}
